package com.xs.lomo;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class StateActivity extends Activity {
	
	private TextView tvBuddyName = null,
					 tvState = null,
					 tvTraffic = null,
					 tvSignal = null;
	private RelativeLayout  rlState = null,
							rlTraffic = null,
							rlSignal = null;
	boolean initFlag = false;
	String BuddyNameStr = null;
	android.os.Message StateMsg = new android.os.Message();    
    Bundle StateBundle = new Bundle();
    
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.state); 
		if(false == initFlag){
			GetTextView();
			IntentFilter intentFilter = new IntentFilter();
	        intentFilter.addAction("lomo.action.state");
	        this.registerReceiver(new StateBroadcastReciver(), intentFilter);
	        initFlag = true;
		}
		InitNameTv(); 
	}
		
	private void InitNameTv(){
		String BuddyStateStr = null;
		Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        BuddyNameStr = bundle.getString("BuddyName"); 
        BuddyStateStr = bundle.getString("BuddyState"); 
		
		tvBuddyName.setText(BuddyNameStr);
		ShowStateView(BuddyStateStr);
	}
	
	private void GetTextView(){
		tvState = (TextView)findViewById(R.id.state_tv);
		tvTraffic = (TextView)findViewById(R.id.traffic_tv);
		tvSignal = (TextView)findViewById(R.id.signal_tv);
		tvBuddyName = (TextView)findViewById(R.id.tvStateBuddyName);
		
		rlState = (RelativeLayout)findViewById(R.id.state_rl);
		rlTraffic = (RelativeLayout)findViewById(R.id.traffic_rl);
		rlSignal = (RelativeLayout)findViewById(R.id.signal_rl);
	}
	
	private void ShowStateView(String StateDataStr){		
		String 	LastStr = StateDataStr;
		String [ ]aStrAttr= new String[4];
		int iPos = 0, iAttr = 0;
		
		// 0:state
		// 1:traffic
		// 2:max traffic
		// 3:signal strength
		if(0 == LastStr.length()){
			return;
		}

		for( ; 0 < LastStr.length(); ){			
			iPos = LastStr.indexOf("|");
			if(-1 == iPos ){
				break;
			}
			aStrAttr[iAttr++] = LastStr.substring(0, iPos);
			LastStr = LastStr.substring(iPos+1);
		}
		
		if(null != aStrAttr[0]){
			tvState.setText(aStrAttr[0]);
			if(getString(R.string.offline_state).equals(aStrAttr[0])){
				rlState.setBackgroundResource(R.color.ErrorState);
			}else{
				rlState.setBackgroundResource(R.color.NormalState);
			}			
		}else{
			tvState.setText(StateDataStr);
			rlState.setBackgroundResource(R.color.ErrorState);
			return ;
		}
		if(null != aStrAttr[1] && null != aStrAttr[2]){
			int iTraffic = Integer.parseInt(aStrAttr[1]);
			int iMaxTraffic = Integer.parseInt(aStrAttr[2]);
			
			tvTraffic.setText(aStrAttr[1]+"/"+aStrAttr[2]+"(M)");
			if(iTraffic >= iMaxTraffic){
				rlTraffic.setBackgroundResource(R.color.ErrorState);
			}else if(iTraffic+5 > iMaxTraffic){
				rlTraffic.setBackgroundResource(R.color.WarningState);
			}else{
				rlTraffic.setBackgroundResource(R.color.NormalState);
			}
		}
		
		if(null != aStrAttr[3]){
			int iSignal = Integer.parseInt(aStrAttr[3]);
			if(32 >= iSignal){
				tvSignal.setText(aStrAttr[3]+"asu");
				if(10 > iSignal){
					rlSignal.setBackgroundResource(R.color.ErrorState);
				}else if(10 <= iSignal && 20 > iSignal){
					rlSignal.setBackgroundResource(R.color.WarningState);
				}else{
					rlSignal.setBackgroundResource(R.color.NormalState);
				}
			}
		}	
	}
   
	private class StateBroadcastReciver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if(action.equals("lomo.action.state")){
				String state = intent.getStringExtra("BuddyState");
				String name = intent.getStringExtra("BuddyName");
				if(BuddyNameStr.equals(name)){
					ShowStateView(state);
				}
			    //StateActivity.this.unregisterReceiver(this);
			}
		}
	}
}